package com.zzz.gp.controller;

import com.zzz.gp.bean.UserVO;
import com.zzz.gp.bean.bo.CommonBo;
import com.zzz.gp.bean.vo.GpQuery;
import com.zzz.gp.bean.vo.TestVO;
import com.zzz.gp.dao.GpMapper;
import com.zzz.gp.service.GpCodeService;
import com.zzz.gp.service.GpHistoryService;
import com.zzz.gp.service.WebSocketServer;
import com.zzz.gp.task.TodayTask;
import com.zzz.gp.utils.HttpRequestUtil;
import com.zzz.gp.utils.RespUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/12/7 12:54
 */
@Slf4j
@RestController
@RequestMapping("/zzz")
public class GpDataController {
    @Autowired
    private GpCodeService gpCodeService;

    @Autowired
    private GpHistoryService gpHistoryService;

    @Autowired
    private GpMapper gpMapper;

    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private TodayTask todayTask;

    @RequestMapping("/ws")
    public String test1(@RequestBody UserVO vo) {
        DateTimeFormatter datedtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String s = LocalDateTime.now().format(datedtf);
        webSocketServer.sendInfo(s, vo.getUId());
        return s;

    }


    @RequestMapping("/test1")
    public CommonBo test1(@RequestBody TestVO vo) {
        String result = "===test seccess===";
        log.info(result);
        return RespUtil.commonResp("000000", "success", "DHFKJHKFH4654DF", vo);
    }


    @RequestMapping("/test")
    public String test() {
        String result = "===test seccess===";
        DateTimeFormatter stf = DateTimeFormatter.ofPattern("yyyyMMdd");
        String s = stf.format(LocalDate.now().minusDays(3));
        String s2 = stf.format(LocalDate.now());
        String url = String.format("https://q.stock.sohu.com/hisHq?code=cn_%s&start=%s&end=%s", 601868, s, s2);
        String info = HttpRequestUtil.httpRequest(url);
        log.info(info);
        return result;
    }

    @RequestMapping("/getCode")
    public String getCode() {
        String result = "====getCode success===";
        log.info(result);

        gpCodeService.SZ1();

        gpCodeService.SZ2(1000, 2000);
        gpCodeService.SZ2(2001, 3000);
        gpCodeService.SZ2(3001, 4000);
        gpCodeService.SZ2(4001, 5000);
        gpCodeService.SZ2(5001, 6000);

        gpCodeService.SZ3(300000, 301000);
        gpCodeService.SZ3(301001, 302500);

        gpCodeService.SH(600000, 601000);
        gpCodeService.SH(601001, 602000);
        gpCodeService.SH(602001, 603000);
        gpCodeService.SH(603001, 604000);
        gpCodeService.SH(604001, 605000);
        gpCodeService.SH(605001, 606000);

        return result;
    }

    @RequestMapping("/getData")
    public void getData() {
        todayTask.todayTask();
    }

    @RequestMapping("/getHistory")
    public String getHistory(@RequestBody GpQuery gpQuery) {
        gpMapper.cleanGpHistory();
        gpHistoryService.batch2(gpQuery);
        return "===getHistory===";

    }

    @RequestMapping("/gp")
    public String getGP(int cleanDay) {
        gpMapper.cleanGpHistory();
        DateTimeFormatter stf = DateTimeFormatter.ofPattern("yyyyMMdd");
        GpQuery gpQuery = new GpQuery();
        String startDate = stf.format(LocalDate.now().minusDays(cleanDay));
        String endDate = stf.format(LocalDate.now());
        gpQuery.setStartDate(startDate);
        gpQuery.setEndDate(endDate);
        gpHistoryService.batch2(gpQuery);
        return "===重新获取数据===";

    }

}

